/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Collections;
using Whitemice.ZOGI;
using Whitemice.ZOGI.GtkWidgets;
using Gtk;
using HollyLibrary;

namespace Whitemice.ZOGI.GtkForms
{
  [System.ComponentModel.Category("Forms")]
  [System.ComponentModel.ToolboxItem(true)]
  public partial class AppointmentDetailForm : Gtk.Bin, IStorableEntityForm, IVerifiedEntityForm
  {
    public AppointmentDetailForm()
    {
      this.Build();
    }

    public void Verify()
    {
      throw new Exception("Appointtment form verification not implmented.");
    }
    
		public bool Take(Entity _entity)
		{
		  Whitemice.ZOGI.Appointment  a;
		  if (_entity is Whitemice.ZOGI.Appointment)
		    a = (_entity as Whitemice.ZOGI.Appointment);
		    else throw new Exception("Improper entity type provided");
		  
		  if (a.CanModify) {
		    a.Title = title.Text;
		    a.Location = location.Text;
		    a.Start = this.Start;
		    a.End = this.End;
		    // Cycle	    
		    //a.Kind = AppointmentType;
		    a.Notification = Reminder;
		    // Read Access
		    // Write Access
		    // Conflicts
		    // Billable
		    // Distance
		    a.Comment = Comment;
		    // Participants
		  }
      return true;
		} /* end Take */
		
		public void Fill(Entity _entity)
		{
		  Whitemice.ZOGI.Appointment a;
		  if (_entity is Whitemice.ZOGI.Appointment)
		    a = (_entity as Whitemice.ZOGI.Appointment);
		    else throw new Exception("Improper entity type provided");
		    
	    a = (_entity as Whitemice.ZOGI.Appointment);
	    /* if new cycle can be edited, existing appointments cannot be edited */
	    if (a.ObjectId < 1)
	    {
	      cycle.Sensitive = true;
	      cycleEnd.Sensitive = true;
	    } else
	      {
	        cycle.Sensitive = false;
	        cycleEnd.Sensitive = false;
	      }
	    /* fill fields */
 		  title.Text = a.Title;
 		  location.Text = a.Location;
 		  startDate.CurrentDate = a.Start;
 		  endDate.CurrentDate = a.End;
 		  // Cycle
 		  //AppointmentType = a.AppointmentType;
 		  Reminder = a.Notification;
 		  // Read
 		  // Write
 		  // Conflicts
 		  // Owner
 		  // Billable
 		  // Parent
 		  // Distance
 		  // Comment
 		  // Participants
		} /* end Fill */
		
		public void Clear()
		{
		  title.Text = string.Empty;
		  location.Text = string.Empty;
		  //start.Text = string.Empty;
		  //end.Text = string.Empty;
		} /* end Clear */

    /* events */
    
		protected virtual void OnCycleChanged (object sender, System.EventArgs e)
		{
		  if (cycle.Active == 0)
		  {
		    cycleEnd.Sensitive = false;
		  } else
		    {
		      cycleEnd.Sensitive = true;
		    }
		} /* end OnCycleChanged */
		

		protected virtual void OnBillableClicked (object sender, System.EventArgs e)
		{
		  if (billable.Active)
		  {
		    this.billId.Sensitive = true;
		    this.billSum.Sensitive = true;
		    this.billCalc.Sensitive = true;
		  } else
		    {
  		    this.billId.Sensitive = false;
  		    this.billSum.Sensitive = false;
  		    this.billCalc.Sensitive = false;
		    }
		} /* end OnBillableClicked */

		protected virtual void OnAddWriterClicked (object sender, System.EventArgs e)
		{
		}

		protected virtual void OnRemoveWriterClicked (object sender, System.EventArgs e)
		{
		}

		protected virtual void OnEndDateDateChanged (object sender, System.EventArgs e)
		{
      CheckDates();
		}

		protected virtual void OnStartDateDateChanged (object sender, System.EventArgs e)
		{
		  CheckDates();
		}

		protected virtual void OnCycleEndDateChanged (object sender, System.EventArgs e)
	  {
	    CheckDates();
		}
		
		/* verifiers */
		
		protected virtual void CheckDates()
		{
      if (startDate.CurrentDate > endDate.CurrentDate) 
      {
        new MessageBox("End time is prior to start time.", MessageType.Error);
      }
		} /* end CheckDates */

		protected virtual void OnBillCalcClicked (object sender, System.EventArgs e)
		{
		}
		
		/* properties */
		
		public DateTime Start { get { return startDate.CurrentDate; } }
		
		public DateTime End { get { return endDate.CurrentDate; } } 
		
		public string Comment { get { return comment.Buffer.Text; } }

		protected string AppointmentType
		{
		  get
		  {
		    switch(type.Active)
		    {
		      case 1: return "all types";
		      case 2: return "at home";
		      case 3: return "birthday";
		      case 4: return "duedate";
		      case 5: return "ill";
		      case 6: return "meeting";
		      case 7: return "outward";
		      case 8: return "phone call";
		      case 9: return "tradeshow";
		      case 10: return "vacation";
		    }
		    return string.Empty;
		  }
		  set
		  {
		    switch(value)
		    {
		      case "all types": type.Active = 1; break;
		      case "at home": type.Active = 2; break;
		      case "birthday": type.Active = 3; break;
		      case "duedate": type.Active = 4; break;
		      case "ill": type.Active = 5; break;
		      case "meeting": type.Active = 6; break;
		      case "outward": type.Active = 7; break;
		      case "phone call": type.Active = 8; break;
		      case "tradeshow": type.Active = 9; break;
		      case "vacation": type.Active = 10; break;
		      default: type.Active = 0; break;
		    }
		  }
		}
		
		protected int Reminder
		{
		  get
		  {
		    switch(cycle.Active)
		    {
		      case 1:  return 5;
		      case 2:  return 10;
		      case 3:  return 30;
		      case 4:  return 60;
		      case 5:  return 120;
		      case 6:  return 360;
		      case 7:  return 720;
		      case 8:  return 1440;
		      case 9:  return 2880;
		      case 10: return 5760;
		      case 11: return 11520;
		    }
		    return 0;
		  }
		  set
		  {
		    switch(value)
		    {
		      case 5: reminder.Active = 1; break;
          case 10: reminder.Active = 2; break;
          case 30: reminder.Active = 3; break;
          case 60: reminder.Active = 4; break;
          case 120: reminder.Active = 5; break;
          case 360: reminder.Active = 6; break;
          case 720: reminder.Active = 7; break;
          case 1440: reminder.Active = 8; break;
          case 2880: reminder.Active = 9; break;
          case 5760: reminder.Active = 10; break;
          case 11520: reminder.Active = 11; break;
          default: reminder.Active = 0; break;
        }
		    if (reminder.Active > 0)
		    {
		      cycleEnd.Sensitive = true;
		    } else
		      {
		        cycleEnd.Sensitive = false;
		      }
		  }
		} // end Reminder
		
		public Gtk.ComboBox Reader
		{
		  get { return reader; }
		}
  }
}
